package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.SelectStudent;
import com.xmy.cultivate.mapper.RadeExcludeStudentMapper;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 率排除学生 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-07-27
 */
@Service
public class RadeExcludeStudentServiceImpl extends ServiceImpl<RadeExcludeStudentMapper, RadeExcludeStudent> implements IRadeExcludeStudentService {
    @Autowired
    RadeExcludeStudentMapper radeExcludeStudentMapper;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    ICourseSchedulingAlwaysService iCourseSchedulingAlwaysService;

    @Autowired
    IOrderDetailService iOrderDetailService;

    @Autowired
    IMqPushService iMqPushService;

    /**
     * 新生
     */
    @Override
    public boolean isNewStudent(Integer yearClassId, Long subjectId, Long studentId, Long schoolId) {
        try {
            Quarter quarter = iQuarterService.getNowQuarter();
            Integer yearPart = quarter.getYearPart();
            Integer quarterNum = quarter.getNum();
            Integer gradeSection = CommonUtil.getGradeSection(yearClassId);

            QueryWrapper wrapper = new QueryWrapper<>();
            wrapper.eq("lesson_type", 1);
            wrapper.eq("subjects_id", subjectId);
            //wrapper.eq("order_type", 1);
            wrapper.ne("status", 2);
            wrapper.eq("student_id", studentId);
            wrapper.eq("year_part", yearPart);
            wrapper.eq("quarter_num", quarterNum);
            Long count = iOrderDetailService.count(wrapper);

            if (count != 0) {
                //System.err.println("不是新生");
                return false;
            }

            Integer num = 0;
            if (quarter.getType() == 1) {
                num = 7;
            } else if (quarter.getType() == 2) {
                num = 3;
            }

            LocalDate endDate = iCourseSchedulingAlwaysService.getEndDate(yearPart, quarterNum, gradeSection, num);
            LocalDate date = LocalDate.now();

            if (endDate != null && date.isAfter(endDate)) {
                wrapper.clear();
                wrapper.eq("classify_id", 0);
                wrapper.eq("year_part", yearPart);
                wrapper.eq("quarter_num", quarterNum);
                wrapper.eq("student_id", studentId);
                wrapper.eq("type", 2);
                wrapper.last("LIMIT 1");
                RadeExcludeStudent radeExcludeStudent = radeExcludeStudentMapper.selectOne(wrapper);
                if (radeExcludeStudent == null) {
                    radeExcludeStudent = new RadeExcludeStudent();
                    radeExcludeStudent.setStudentId(studentId);
                    radeExcludeStudent.setYearPart(yearPart);
                    radeExcludeStudent.setQuarterNum(quarterNum);
                    radeExcludeStudent.setType(2);
                    //radeExcludeStudent.setSchoolIds(String.valueOf(schoolId));
                    radeExcludeStudent.setSubjectsIds(String.valueOf(subjectId));
                    radeExcludeStudent.setClassifyId(0L);
                    radeExcludeStudentMapper.insert(radeExcludeStudent);
                    iMqPushService.listRadeExcludeStudent(radeExcludeStudent);
                } else {
                    //String schoolIds = String.join(",", radeExcludeStudent.getSchoolIds(), String.valueOf(schoolId));
                    //radeExcludeStudent.setSchoolIds(schoolIds);
                    if (!radeExcludeStudent.getSubjectsIds().contains(subjectId.toString())){
                        String subjectIds = String.join(",", radeExcludeStudent.getSubjectsIds(), String.valueOf(subjectId));
                        radeExcludeStudent.setSubjectsIds(subjectIds);
                        radeExcludeStudentMapper.updateById(radeExcludeStudent);
                        iMqPushService.listRadeExcludeStudent(radeExcludeStudent);
                    }
                }
            } else {
                //System.err.println("当前日期不大于课次结束日期");
                return false;
            }
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Async("onlineAsync")
    @Override
    public void addOldData(){
        Quarter quarter = iQuarterService.getNowQuarter();
        Integer yearPart = quarter.getYearPart();
        Integer quarterNum = quarter.getNum();
        Integer gradeSection = CommonUtil.getGradeSection(6);
        LocalDate endDate = iCourseSchedulingAlwaysService.getEndDate(yearPart, quarterNum, gradeSection, 3);

        QueryWrapper<OrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
        orderDetailQueryWrapper.eq("year_part", yearPart);
        orderDetailQueryWrapper.eq("quarter_num", quarterNum);
        orderDetailQueryWrapper.eq("lesson_type", 1);
        orderDetailQueryWrapper.gt("handle_date",endDate);
        orderDetailQueryWrapper.groupBy("subjects_id","student_id");
        List<OrderDetail> orderDetailList = iOrderDetailService.list(orderDetailQueryWrapper);
        for (OrderDetail orderDetail : orderDetailList) {
            orderDetailQueryWrapper.clear();
            orderDetailQueryWrapper.eq("year_part", yearPart);
            orderDetailQueryWrapper.eq("quarter_num", quarterNum);
            orderDetailQueryWrapper.eq("lesson_type", 1);
            orderDetailQueryWrapper.le("handle_date",endDate);
            orderDetailQueryWrapper.eq("student_id", orderDetail.getStudentId());
            orderDetailQueryWrapper.eq("subjects_id", orderDetail.getSubjectsId());
            Long count = iOrderDetailService.count(orderDetailQueryWrapper);
            if (count>0){
            }else {
                Long studentId = orderDetail.getStudentId();
                QueryWrapper wrapper = new QueryWrapper<>();
                wrapper.eq("classify_id", 0);
                wrapper.eq("year_part", yearPart);
                wrapper.eq("quarter_num", quarterNum);
                wrapper.eq("student_id", studentId);
                wrapper.eq("type", 2);
                wrapper.last("LIMIT 1");
                RadeExcludeStudent radeExcludeStudent = radeExcludeStudentMapper.selectOne(wrapper);
                if (radeExcludeStudent == null) {
                    radeExcludeStudent = new RadeExcludeStudent();
                    radeExcludeStudent.setStudentId(studentId);
                    radeExcludeStudent.setYearPart(yearPart);
                    radeExcludeStudent.setQuarterNum(quarterNum);
                    radeExcludeStudent.setType(2);
                    //radeExcludeStudent.setSchoolIds(String.valueOf(schoolId));
                    radeExcludeStudent.setSubjectsIds(String.valueOf(orderDetail.getSubjectsId()));
                    radeExcludeStudent.setClassifyId(0L);
                    radeExcludeStudentMapper.insert(radeExcludeStudent);
                } else {
                    //String schoolIds = String.join(",", radeExcludeStudent.getSchoolIds(), String.valueOf(schoolId));
                    //radeExcludeStudent.setSchoolIds(schoolIds);
                    if (!radeExcludeStudent.getSubjectsIds().contains(orderDetail.getSubjectsId().toString())){
                        String subjectIds = String.join(",", radeExcludeStudent.getSubjectsIds(), String.valueOf(orderDetail.getSubjectsId()));
                        radeExcludeStudent.setSubjectsIds(subjectIds);
                        radeExcludeStudentMapper.updateById(radeExcludeStudent);
                    }
                }
            }
        }
    }

    @Override
    public IPage<SelectStudent> getRadeExcludeStudentStudent(IPage<SelectStudent> page, Wrapper wrapper) {
        return radeExcludeStudentMapper.getRadeExcludeStudentStudent(page, wrapper);
    }

}
